/*
	插件模块
*/
import config from "../config/config.js"
const Lib = function() {}
import vue from 'vue'
import print from './print.js'
const _this = new vue()
import {
	login,
	getCode,
	checkToken,
	checkStore,
	setLocalStore
} from "./login.js"
Lib.prototype = {
	async globalResponseCatch(res, code, message) {
		switch (code) {
			case 403:
				//重新登陆
				console.log(code, 'code')
				// print('403重新登陆');
				// uni.startPullDownRefresh()
				uni.removeStorageSync('AUTH_TOKEN')
				uni.removeStorageSync('USER')
				uni.redirectTo({
					url: '/public/login/login',
				})
				// await login();
				// uni.hideLoading();
				// uni.stopPullDownRefresh()
				throw new Error(403)
				break;
			case 500:
				//500异常
				uni.showModal({
					title: "没有请求成功～",
					content: message,
				})
				uni.hideLoading();
				uni.stopPullDownRefresh()
				throw new Error(500)
				break;
			case 404:
				print('404异常');
				uni.hideLoading();
				uni.stopPullDownRefresh()
				throw new Error(404)
				break;
			case 406:
				print('406手机号未完善');
				uni.navigateTo({
					url: '/public/publicPhone/publicPhone'
				})
				throw new Error(406)
				break;
			case 408:
				print('您已经是律师或正在审核中,请勿重复提交');
				throw new Error(408)
				break;
			case 409:
				print('409需要验证身份证号码');
				uni.showModal({
					title: '提示',
					content: '您还没有认证身份证信息，请先实名认证',
					// success(res) {
					// 	if (res.confirm) {
					// 		uni.navigateTo({
					// 			url: "/public/publicRealName/publicRealName"
					// 		})
					// 		return
					// 	}
					// 	uni.navigateBack();
					// }
				})
				// public/publicRealName/publicRealName
				break;
			default:
				return res;
				break;
		}
	},
	async $post(obj, showLoading) {
		const _this = this;
		const token = (await _this.chackToken()).data;

		if (token) {
			obj.header = {
				token,
			}
		}
		if (showLoading) {
			uni.showLoading({
				title: '加载中...',
				icon: 'none'
			})
		}

		return await new Promise((resolve) => {
			obj.method = "POST";
			obj.success = async function(res) {
				uni.hideLoading();
				const afterGlobalCatch = await _this.globalResponseCatch(res, res.data.code, res.data.message);
				const resCatched = obj.responseCatch(afterGlobalCatch, afterGlobalCatch.data.code, afterGlobalCatch.data.message)
				resolve(resCatched);
			};
			uni.request(obj)
		})
	},
	async $get(obj, showLoading) {
		const _this = this;
		const token = (await _this.chackToken()).data;
		if (token) {
			obj.header = {
				token,
			}
		}
		if (showLoading) {
			uni.showLoading({
				title: '加载中...',
				icon: 'none'
			})
		}
		return await new Promise((resolve) => {
			obj.method = "GET";
			obj.success = async function(res) {
				uni.hideLoading()
				const afterGlobalCatch = await _this.globalResponseCatch(res, res.data.code, res.data.message);
				const resCatched = obj.responseCatch(afterGlobalCatch, afterGlobalCatch.data.code, afterGlobalCatch.data.message)
				resolve(resCatched);
			};
			uni.request(obj)
		})
	},
	async $upload(obj, showLoading) {
		const _this = this;
		const token = (await _this.chackToken()).data;
		if (token) {
			obj.header = {
				token,
			}
		}
		uni.showLoading({
			title: '加载中...',
			icon: 'none'
		})

		return await new Promise((resolve) => {
			console.log(obj, 'obj')
			obj.success = async function(res) {
				uni.hideLoading()
				const afterGlobalCatch = await _this.globalResponseCatch(res, res.data.code, res.data.message);
				const resCatched = obj.responseCatch(afterGlobalCatch, afterGlobalCatch.data.code, afterGlobalCatch.data.message)
				resolve(resCatched);
			};
			obj.fail = async function(err) {
				console.log(err)
			};
			obj.name = 'file'
			obj.filePath = obj.data.filePath

			uni.uploadFile(obj)

		})
	},
	async chackToken() {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: 'AUTH_TOKEN',
				success: function(res) {
					resolve(res)
				},
				fail: function(err) {
					resolve({
						data: ''
					})
				}
			});
		})
	}
}
export default Lib
